﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Innova.Microfinanzas.BusinessEntities;

namespace Innova.Microfinanzas.Business.UsersAndSecurity
{
    public class RoleManager
    {
        private MyCommerceEntities vContext;

        public RoleManager()
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = false;
            }
        }

        public RoleManager(bool lazyLoad)
        {
            if (vContext == null)
            {
                vContext = new MyCommerceEntities();
                vContext.ContextOptions.LazyLoadingEnabled = lazyLoad;
            }
        }

        public List<Role> GetRoleActives()
        {
            List<Role> colRoles = new List<Role>();

            var result = from rl in vContext.Role
                         where rl.IsActive == true
                         select rl;
            if (result != null)
                colRoles = new List<Role>(result.AsEnumerable<Role>());

            return colRoles;
        }

        public List<Role> GetRolesInSystem()
        {
            List<Role> colRoles = new List<Role>();

            var result = from rl in vContext.Role
                         select rl;
            if (result != null)
                colRoles = new List<Role>(result.AsEnumerable<Role>());

            return colRoles;
        }

        public void CreateRole(Role objRole)
        {
            vContext.AddObject("Role", objRole);
            vContext.SaveChanges();
        }

        public void UpdateRole(Role objRole)
        {
            var vResult = (from us in vContext.Role
                           where us.IdRole == objRole.IdRole
                           select us).First();

            vResult.IdRole = objRole.IdRole;
            vResult.IsActive = objRole.IsActive;
            vResult.RoleName = objRole.RoleName;

            vContext.SaveChanges();
        }

        public void DeleteRole(Role objRole)
        {
            var vResult = (from us in vContext.Role
                           where us.IdRole == objRole.IdRole
                           select us).First();
            vContext.DeleteObject(vResult);
            vContext.SaveChanges();
        }
    }
}
